//通用上传组件
//../service/upload?path=demo/20130101  path为上传路径，不要带中文字符
function upload() {
	$.ajaxFileUpload({
		url : '../service/upload?path=demo/20130101',
		dataType : 'json',
		data : {param1:'参数1', param2:'参数2'},
		secureuri : false,
		fileElementId : 'afile',
		success : function(data, status) {
			if(data.result == 'success') {
				alert(JSON.stringify(data));
			} else {
				alert(data.result);
			}
		},
		error : function(data, status, e) {
			alert(e);
		}
	});
}

function getFiles() {
	var path = "demo/20130101";
	$.ajax({
		type : "post",
		data : {
			path : path
		},
		dataType : "json",
		// url : "public?/getTyphoonAllPathById",
		url : "getFileList!test2",
		success : function(json) {
			$(json).each(function() {
				var li = '<li id="tf-' + this.filename + '"> ' + this.filename + '</li>';
				$("#filelist").append(li);
			});
			$("#filelist li").click(function() {
				var id = this.id;
				download(path, id.split("-")[1]);
			});
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			// alert(errorThrown);
		}
	});	
}

function download(path, id) {
	var elemIF = document.createElement("iframe");
	elemIF.src = "../service/download?file="+id+"&path="+path;
	elemIF.style.display = "none";
	document.body.appendChild(elemIF);
}

$(document).ready(function() {
	getFiles();
	$('#keyword').bind('keyup', function(event) {
		if(event.keyCode == 13) {
			upload();
		}
	});
});